import 'package:flutter/material.dart';
import 'package:flutter_module/base/base.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class GetPageController extends GetxController {
  var count = 0;

  RxInt countObx = 0.obs;

  void upCount() {
    count++;
    print(count);
    update();
  }

  void upCountObx() {
    countObx.value++;
  }
}

/// 不推荐: GetView 内部 其实是直接 find GetXController 相当与加入全局变量
class GetStaticWidget extends GetView<GetPageController> {
  const GetStaticWidget({super.key});

  @override
  Widget build(BuildContext context) {
    GetPageController c = GetInstance().put(GetPageController());
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Text(
          "GetX-GetView",
          style: TextStyle(color: Colors.black, fontSize: 36.w),
        ),
        Expanded(child: Container()),
        GetBuilder(
          init: controller,
          global: false,
          builder: (controller) {
            return Text(
              "${controller.count}",
              style: TextStyle(color: Colors.black, fontSize: 36.w),
            );
          },
        ),
        GestureDetector(
          onTap: () {
            controller.upCount();
          },
          child: Text(
            "点击事件",
            style: TextStyle(color: Colors.amber, fontSize: 48.w),
          ),
        ),
        Obx(
          () => Text(
            "${controller.countObx}",
            style: TextStyle(color: Colors.black, fontSize: 36.w),
          ),
        ),
        GestureDetector(
          onTap: () {
            controller.upCountObx();
          },
          child: Text(
            "点击事件 Obx",
            style: TextStyle(color: Colors.amber, fontSize: 48.w),
          ),
        ),
        Expanded(child: Container()),
      ],
    );
  }
}

class GetPage extends BaseStateWidget {
  GetPage(this.vm, {super.key});

  GetPageController vm;

  @override
  BaseState<BaseStateWidget> buildState() => GetPageState();
}

/// 继承有状态的
class GetPageState extends BaseState<GetPage> {
  @override
  Widget setContent(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Text(
          "GetX",
          style: TextStyle(color: Colors.black, fontSize: 36.w),
        ),
        Expanded(child: Container()),
        GetBuilder(
          init: widget.vm,
          global: false,
          builder: (controller) {
            return Text(
              "${controller.count}",
              style: TextStyle(color: Colors.black, fontSize: 36.w),
            );
          },
        ),
        GestureDetector(
          onTap: () {
            widget.vm.upCount();
          },
          child: Text(
            "点击事件",
            style: TextStyle(color: Colors.amber, fontSize: 48.w),
          ),
        ),
        Obx(
          () => Text(
            "${widget.vm.countObx}",
            style: TextStyle(color: Colors.black, fontSize: 36.w),
          ),
        ),
        GestureDetector(
          onTap: () {
            widget.vm.upCountObx();
          },
          child: Text(
            "点击事件 Obx",
            style: TextStyle(color: Colors.amber, fontSize: 48.w),
          ),
        ),
        Expanded(child: Container()),
      ],
    );
  }
}

/// 静态
class GetWidget extends BaseWidget {
  const GetWidget({super.key});

  @override
  Widget setContent(BuildContext context) {
    return Column(
      children: [
        Expanded(child: GetPage(GetPageController())),
        Expanded(child: GetStaticWidget()),
      ],
    );
  }
}
